// subpackage/pages/userRegisterV2/index.js
import requestUtil from '../../../baseRequest'
import urls from '../../../requestUrl'
let h;
let w;
wx.getSystemInfo({
  success: function (res) {
    h = res.windowHeight
  },
})
Page({
  data: {
    backImageHeight: h,
    ifRegister: false,
    imageHeight: 0,
    bg_url: urls.baseImagePrefixUrl + '/cac54a53-fa2e-40b2-8e06-e893feaacd26.png',

  },
  //先微信登录获取CODE，再调用后台接口获取用户信息；
  getWxOpenId() {
    wx.login({
      success: res => {
        requestUtil.getRequest(urls.user.getCode + res.code, {}, (res) => {
          wx.setStorageSync('openid', res.openid);
          wx.setStorageSync("isMember", res.is_member);
          if (res.isRegister === 1) {
            this.setData({
              ifRegister: true,
            })
          };
          if(res.hasPay==='Y'){
            wx.showModal({
              title: '温馨提示',
              content: '检查到您已注册成功,不能重复注册',
              showCancel: false,
              success: res => {
                if (res.confirm) {
                  wx.switchTab({
                    url: '/pages/user/personal/index',
                  })
                }
              }
            })
          }
          // if(res.isDriver>-1){
          //   wx.showModal({
          //     title: '温馨提示',
          //     content: '检查到您已注册成功,不能重复注册',
          //     showCancel: false,
          //     success: res => {
          //       if (res.confirm) {
          //         wx.switchTab({
          //           url: '/pages/user/personal/index',
          //         })
          //       }
          //     }
          //   })
          // }
        });
      }
    })
  },
  handleGetUserInfo({detail}) {
    if (detail.userInfo) {
      this.updateUserInfo(detail.userInfo)
      wx.redirectTo({
        url: '/subpackage/pages/userRegisterV2/stepTwo',
      });
    } else {
      wx.showModal({
        title: '授权失败',
        content: '无法获取您的公开信息(昵称，头像等),请重新授权',
        showCancel: false
      })
    }
  },
  updateUserInfo(info) {
    console.log(info)
    let param = {
      "headimgUrl": info.avatarUrl,
      "nickname": info.nickName,
      "openid": wx.getStorageSync('openid')
    }
    requestUtil.postRequest(urls.user.updateInfo, param, (res) => {
      if (res && (res.code == 10014)) {
        getApp().userLogin(() => {
          getApp().updateGlobalUserInfo(1, info.avatarUrl, info.nickName)
          requestUtil.postRequest(urls.user.updateInfo, param, (res) => {
          })
        })
      } else {
        getApp().updateGlobalUserInfo(1, info.avatarUrl, info.nickName)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getWxOpenId();
    let distributeCode = options.scene;
        if (distributeCode) {
            getApp().saveDistributeCode(distributeCode);
        }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})